/*
 * Copyright (c) 2024 Huawei Technologies Co.,Ltd.
 *
 * openGauss is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *
 * http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 * ---------------------------------------------------------------------------------------
 *
 * imcs_cache_mgr.h
 * routines to support common cache
 *
 * IDENTIFICATION
 * src/include/access/imcs/imcs_cache_mgr.h
 *
 * ---------------------------------------------------------------------------------------
 */

#ifndef IMCSCacheMgr_H
#define IMCSCacheMgr_H

#include "postgres.h"
#include "knl/knl_variable.h"
#include "utils/hsearch.h"
#include "storage/lock/lwlock.h"
#include "storage/spin.h"
#include "storage/cache_mgr.h"

int IMCacheMgrNumLocks(int64 cache_size, uint32 each_block_size);
int64 IMCacheMgrCalcSizeByType(MgrCacheType type);

/*
 * This class is to manage Common Cache.
 */
class IMCSCacheMgr : public CacheMgr {
public:
    IMCSCacheMgr(){};
    ~IMCSCacheMgr(){};
    void Init(int64 cache_size, uint32 each_block_size, MgrCacheType type, uint32 each_slot_length);
    void Destroy(void);

    bool PinCacheBlock(CacheSlotId_t slotId);
    void UnPinCacheBlock(CacheSlotId_t slotId);
    CacheDesc *GetCacheDesc(CacheSlotId_t slotId);

#ifndef ENABLE_UT
private:
#endif // ENABLE_UT

    /* internal block operate */
    CacheSlotId_t EvictCacheBlock(int size, int retryNum);

    /* memory operate */
    void FreeCacheBlockMem(CacheSlotId_t slotId);

    void PinCacheBlock_Locked(CacheSlotId_t slotId);
};

#endif /* IMCSCacheMgr_H */
